package com.marion.study.controller;

import com.marion.study.aop.AdminOnly;
import com.marion.study.entity.UserEntity;
import com.marion.study.service.AuthService;
import com.marion.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AuthService authService;

    @GetMapping("/user/login")
    public Object user() {
        HashMap<Object, Object> ret = new HashMap<>();

        List<UserEntity> rus = userService.getUserFromRead();

        // 登陆
        rus.stream().forEach(v -> {
            authService.login(v);
        });

        ret.put("rus", rus);

        return ret;
    }

    /**
     * 主页
     */
    @GetMapping("/user/index")
    @AdminOnly
    public Object index() {

        HashMap<Object, Object> ret = new HashMap<>();

        List<UserEntity> wus = userService.getUserFromWrite();
        ret.put("wus", wus);

        return ret;
    }

    @GetMapping("/user/exit")
    @AdminOnly
    public Object exit() {

        HashMap<Object, Object> ret = new HashMap<>();

        authService.userExit();

        ret.put("status", "ok");

        return ret;
    }

}
